//
//  WQComposeTextView.swift
//  WeiBo
//
//  Created by 张德华 on 7/26/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit

class WQComposeTextView: UITextView {
    
    override var font: UIFont? {
        didSet{
            placeholderLabel.font = font
        }
    }
    
    var placeholder: String? {
        didSet{
            placeholderLabel.text = placeholder
        }
    }
    
    
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI() {
        
        addSubview(placeholderLabel)
        
        placeholderLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(5)
            make.width.equalTo(SCREENSIZE.width-10)
            make.top.equalTo(8)
            
        }
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(WQComposeTextView.valueChanged), name: UITextViewTextDidChangeNotification, object: nil)
        
    }
    
    func valueChanged() {
        placeholderLabel.hidden = self.hasText()
    }
    
    
    
    lazy var placeholderLabel: UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        return label
    }()
    
    
}
